//
//  PersonalInfoVC.m
//  MiTu
//
//  Created by CY on 15/11/12.
//  Copyright © 2015年 CY. All rights reserved.
//

#import "PersonalInfoVC.h"

@interface PersonalInfoVC ()<UITextFieldDelegate>

@property (nonatomic, strong) UIView * topView;
@property (nonatomic, strong) UIButton * returnBtn;
@property (nonatomic, strong) UIImageView * metooIV;
@property (nonatomic, strong) UIButton * iconBtn;
@property (nonatomic, strong) UIButton * setIconBtn;
@property (nonatomic, strong) UIButton * cameraBtn;

@property (nonatomic, strong) UILabel * personalInfoLbl;
@property (nonatomic, strong) UILabel * nickNameLbl;
@property (nonatomic, strong) UILabel * sexLbl;
@property (nonatomic, strong) UILabel * newpasswordLbl;
@property (nonatomic, strong) UILabel * passwordConfirmedLbl;
@property (nonatomic, strong) UIImageView * maleIV;
@property (nonatomic, strong) UIButton * maleBtn;
@property (nonatomic, strong) UIImageView * femaleIV;
@property (nonatomic, strong) UIButton * femaleBtn;
@property (nonatomic, strong) UIButton * confirmedBtn;

@property (nonatomic, strong) UITextField * textField1; // 昵称
@property (nonatomic, strong) UITextField * textField2; // 新密码
@property (nonatomic, strong) UITextField * textField3; // 确认密码

@property (nonatomic, strong) UILabel * remindLbl1; // 昵称不能为空
@property (nonatomic, strong) UILabel * remindLbl2; // 密码不能为空
@property (nonatomic, strong) UILabel * remindLbl3; // 前后密码不一致

// 存储用户输入的个人信息
@property (nonatomic, strong) NSString * nickName;
@property (nonatomic, assign) BOOL sexChosen;
@property (nonatomic, assign) BOOL isMale;
@property (nonatomic, strong) NSString * tempPassword; // 第一次输入的密码
@property (nonatomic, strong) NSString * password; // 第二次确认的密码

@end

@implementation PersonalInfoVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUI];
    [self setupConstraints];
}

- (void)setUI
{
    [self.view addSubview:self.topView];
    
    [self.view addSubview:self.personalInfoLbl];
    [self.view addSubview:self.nickNameLbl];
    [self.view addSubview:self.sexLbl];
    [self.view addSubview:self.newpasswordLbl];
    [self.view addSubview:self.passwordConfirmedLbl];
    [self.view addSubview:self.maleIV];
    [self.view addSubview:self.maleBtn];
    [self.view addSubview:self.femaleIV];
    [self.view addSubview:self.femaleBtn];
    [self.view addSubview:self.confirmedBtn];

    [self.view addSubview:self.remindLbl1];
    [self.view addSubview:self.remindLbl2];
    [self.view addSubview:self.remindLbl3];
}

- (void)setupConstraints
{
    NSDictionary * variableBindings = NSDictionaryOfVariableBindings(_topView, _metooIV, _iconBtn, _setIconBtn, _cameraBtn, _personalInfoLbl, _nickNameLbl, _sexLbl, _newpasswordLbl, _passwordConfirmedLbl, _maleIV, _maleBtn, _femaleIV, _femaleBtn, _confirmedBtn);
    //topView
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_topView]-0-|" options:0 metrics:0 views:variableBindings]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_topView(height)]" options:0 metrics:@{@"height":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:231.f])} views:variableBindings]];
    //returnBtn
    
    //me_too
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.metooIV attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_metooIV(height1)]-(height2)-|" options:0 metrics:@{@"height1":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:30.5f]),@"height2":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:11.5f])} views:variableBindings]];
    //iconBtn
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_iconBtn(height1)]-(height2)-[_metooIV]" options:NSLayoutFormatAlignAllCenterX metrics:@{@"height1":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:115.f]),@"height2":@([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:17.5f])} views:variableBindings ]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.iconBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.iconBtn attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
    //setIconBtn
    CGFloat k = [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:5];
    [MTConstraintHelper setView:self.setIconBtn fullAsSuperview:self.iconBtn andEdgeInsets:UIEdgeInsetsMake(k, k, k, k)];
    //camera
    [MTConstraintHelper setView:self.cameraBtn fullAsSuperview:self.topView andEdgeInsets:UIEdgeInsetsMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:158], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:217], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:231-158-19], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:375-217-31.5])];
    //个人信息
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.personalInfoLbl attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:19.f]]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.personalInfoLbl attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    
    [self.view addSubview:self.returnBtn];
    [self.view addSubview:self.textField1];
    [self.view addSubview:self.textField2];
    [self.view addSubview:self.textField3];
    
}

#pragma mark - Property
- (UIView *)topView
{
    if (!_topView) {
        _topView = [UIView autolayoutView];
        _topView.backgroundColor = [MTColor colorWithRGB:0xF8B62C];
        [_topView addSubview:self.returnBtn];
        [_topView addSubview:self.metooIV];
        [_topView addSubview:self.iconBtn];
        [_topView addSubview:self.cameraBtn];
    }
    return _topView;
}

- (UIButton *)returnBtn
{
    if (!_returnBtn) {
        _returnBtn = [[UIButton alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:25], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:30], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:30])];
        [_returnBtn setImage:[UIImage imageNamed:@"return2"] forState:UIControlStateNormal];
        [_returnBtn addTarget:self action:@selector(returnBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _returnBtn;
}

- (UIImageView *)metooIV
{
    if (!_metooIV) {
        _metooIV = [UIImageView autolayoutView];
        _metooIV.image = [UIImage imageNamed:@"me_too2"];
    }
    return _metooIV;
}

- (UIButton *)iconBtn
{
    if (!_iconBtn) {
        _iconBtn = [UIButton autolayoutView];
        [_iconBtn addSubview:self.setIconBtn];
        _iconBtn.backgroundColor = [UIColor whiteColor];
        [_iconBtn addTarget:self action:@selector(iconChanged:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _iconBtn;
}

- (UIButton *)setIconBtn
{
    if (!_setIconBtn) {
        _setIconBtn = [UIButton autolayoutView];
        _setIconBtn.backgroundColor = [MTColor colorWithRGB:0xB4B5B5];
        [_setIconBtn setTitle:@"请设置头像" forState:UIControlStateNormal];
        _setIconBtn.titleLabel.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:18.f]];
        [_setIconBtn addTarget:self action:@selector(iconChanged:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _setIconBtn;
}

- (UIButton *)cameraBtn
{
    if (!_cameraBtn) {
        _cameraBtn = [UIButton autolayoutView];
        [_cameraBtn setImage:[UIImage imageNamed:@"camera"] forState:UIControlStateNormal];
        [_cameraBtn addTarget:self action:@selector(iconChanged:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cameraBtn;
}

- (UILabel *)personalInfoLbl
{
    if (!_personalInfoLbl) {
        _personalInfoLbl = [UILabel autolayoutView];
        _personalInfoLbl.text = @"个人信息";
        _personalInfoLbl.textColor = [MTColor colorWithRGB:0xF8B62C];
        _personalInfoLbl.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:18.f]];
    }
    return _personalInfoLbl;
}

- (UILabel *)nickNameLbl
{
    if (!_nickNameLbl) {
        _nickNameLbl = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:45], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:290], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:70], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15])];
        _nickNameLbl.text = @"昵称";
        _nickNameLbl.textAlignment = NSTextAlignmentRight;
        _nickNameLbl.textColor = [MTColor colorWithRGB:0xF8B62C];
        _nickNameLbl.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15.f]];
    }
    return _nickNameLbl;
}

- (UILabel *)sexLbl
{
    if (!_sexLbl) {
        _sexLbl = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:45], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:343], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:70], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15])];
        _sexLbl.text = @"性别";
        _sexLbl.textAlignment = NSTextAlignmentRight;
        _sexLbl.textColor = [MTColor colorWithRGB:0xF8B62C];
        _sexLbl.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15.f]];
    }
    return _sexLbl;
}

- (UILabel *)newpasswordLbl
{
    if (!_newpasswordLbl) {
        _newpasswordLbl = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:45], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:396], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:70], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15])];
        _newpasswordLbl.text = @"新密码";
        _newpasswordLbl.textAlignment = NSTextAlignmentRight;
        _newpasswordLbl.textColor = [MTColor colorWithRGB:0xF8B62C];
        _newpasswordLbl.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15.f]];
    }
    return _newpasswordLbl;
}

- (UILabel *)passwordConfirmedLbl
{
    if (!_passwordConfirmedLbl) {
        _passwordConfirmedLbl = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:45], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:449], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:70], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15])];
        _passwordConfirmedLbl.text = @"确认密码";
        _passwordConfirmedLbl.textAlignment = NSTextAlignmentRight;

        _passwordConfirmedLbl.textColor = [MTColor colorWithRGB:0xF8B62C];
        _passwordConfirmedLbl.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15.f]];
    }
    return _passwordConfirmedLbl;
}

- (UIImageView *)maleIV
{
    if (!_maleIV) {
        _maleIV = [[UIImageView alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:167], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:343], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:18], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:20])];
        _maleIV.image = [UIImage imageNamed:@"male"];
    }
    return _maleIV;
}

- (UIButton *)maleBtn
{
    if (!_maleBtn) {
        _maleBtn = [[UIButton alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:140], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:346], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10])];
        _maleBtn.tag = 100;
        
        _maleBtn.layer.masksToBounds = YES;
        _maleBtn.layer.cornerRadius = [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:5];
        _maleBtn.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _maleBtn.layer.borderWidth = 1;
        
        [_maleBtn addTarget:self action:@selector(sexChosen:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _maleBtn;
}

- (UIImageView *)femaleIV
{
    if (!_femaleIV) {
        _femaleIV = [[UIImageView alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:243], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:343], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:14], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:21])];
        _femaleIV.image = [UIImage imageNamed:@"female"];
    }
    return _femaleIV;
}

- (UIButton *)femaleBtn
{
    if (!_femaleBtn) {
        _femaleBtn = [[UIButton alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:219], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:346], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10])];
        _femaleBtn.tag = 101;
        _femaleBtn.layer.masksToBounds = YES;
        _femaleBtn.layer.cornerRadius = [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:5];
        _femaleBtn.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        _femaleBtn.layer.borderWidth = 1;
        
        [_femaleBtn addTarget:self action:@selector(sexChosen:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _femaleBtn;
}

- (UIButton *)confirmedBtn
{
    if (!_confirmedBtn) {
        _confirmedBtn = [[UIButton alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:128.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:500.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:143.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:25.f])];
        [_confirmedBtn setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
        [_confirmedBtn setBackgroundImage:[UIImage imageWithColor:[MTColor colorWithRGB:0xF8B62C]] forState:UIControlStateNormal];
        [_confirmedBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_confirmedBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmedBtn.titleLabel.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:15.f]];
        _confirmedBtn.layer.masksToBounds = YES;
        _confirmedBtn.layer.cornerRadius = 4;
        [_confirmedBtn addTarget:self action:@selector(confirmedBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        _confirmedBtn.enabled = NO;
    }
    return _confirmedBtn;
}

- (UILabel *)remindLbl1
{
    if (!_remindLbl1) {
        _remindLbl1 = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:0], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:313], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:100], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:12])];
        _remindLbl1.right = [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:267];
        _remindLbl1.text = @"昵称不能为空";
        _remindLbl1.textColor = [UIColor redColor];
        _remindLbl1.textAlignment = NSTextAlignmentRight;
        _remindLbl1.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10.f]];
        _remindLbl1.hidden = YES;
    }
    return _remindLbl1;
}

- (UILabel *)remindLbl2
{
    if (!_remindLbl2) {
        _remindLbl2 = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:0], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:418], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:100], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:12])];
        _remindLbl2.right = [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:267];
        _remindLbl2.text = @"密码不能为空";
        _remindLbl2.textColor = [UIColor redColor];
        _remindLbl2.textAlignment = NSTextAlignmentRight;
        _remindLbl2.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10.f]];
        _remindLbl2.hidden = YES;
    }
    return _remindLbl2;
}

- (UILabel *)remindLbl3
{
    if (!_remindLbl3) {
        _remindLbl3 = [[UILabel alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:0], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:472], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:100], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:12])];
        _remindLbl3.right = [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:267];
        _remindLbl3.text = @"前后密码不一致";
        _remindLbl3.textColor = [UIColor redColor];
        _remindLbl3.textAlignment = NSTextAlignmentRight;
        _remindLbl3.font = [UIFont systemFontOfSize:[MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:10.f]];
        _remindLbl3.hidden = YES;
    }
    return _remindLbl3;
}

- (UITextField *)textField1
{
    if (!_textField1) {
        _textField1 = [[UITextField alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:128.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:285.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:143.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:25.f])];
        _textField1.text = self.registerPhoneNum;
        self.nickName = self.registerPhoneNum;
        _textField1.font = [UIFont systemFontOfSize:12];
        _textField1.borderStyle = UITextBorderStyleRoundedRect;
        _textField1.clearButtonMode = UITextFieldViewModeAlways;
        _textField1.tintColor = [UIColor lightGrayColor]; // 光标颜色
        
        _textField1.delegate = self;
    }
    return _textField1;
}

- (UITextField *)textField2
{
    if (!_textField2) {
        _textField2 = [[UITextField alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:128.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:390.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:143.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:25.f])];
        _textField2.layer.borderColor = [[UIColor blackColor] CGColor];
        _textField2.borderStyle = UITextBorderStyleRoundedRect;
        _textField2.clearButtonMode = UITextFieldViewModeAlways;
        _textField2.tintColor = [UIColor lightGrayColor];
        _textField2.secureTextEntry = YES; // 密码
        
        _textField2.delegate = self;

    }
    return _textField2;
}

- (UITextField *)textField3
{
    if (!_textField3) {
        _textField3 = [[UITextField alloc] initWithFrame:CGRectMake([MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:128.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:445.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:143.f], [MTSize getLengthWithSizeType:OTSSizeType4_7 andLength:25.f])];
        _textField3.borderStyle = UITextBorderStyleRoundedRect;
        _textField3.clearButtonMode = UITextFieldViewModeAlways;
        _textField3.tintColor = [UIColor lightGrayColor]; // 光标颜色
        _textField3.secureTextEntry = YES;
        
        _textField3.delegate = self;

    }
    return _textField3;
}

#pragma mark - Action
- (void)returnBtnClicked:(UIButton *)button
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (void)iconChanged:(UIButton *)button
{
    [self.iconBtn setImage:[UIImage imageNamed:@"me_too"] forState:UIControlStateNormal];
    self.setIconBtn.hidden = YES;
}

- (void)sexChosen:(UIButton *)button
{
    self.sexChosen = YES;
    if (button.tag == 100) {
        [_maleBtn setBackgroundColor:[MTColor colorWithRGB:0xF8B62C]];
        [_femaleBtn setBackgroundColor:[UIColor whiteColor]];
        self.isMale = YES;
    } else if (button.tag == 101) {
        [_femaleBtn setBackgroundColor:[MTColor colorWithRGB:0xF8B62C]];
        [_maleBtn setBackgroundColor:[UIColor whiteColor]];
        self.isMale = NO;
    }
    [self judgeInfo];
}

- (void)confirmedBtnClicked:(UIButton *)button
{
    [MTGlobalValue sharedInstance].nickName = self.nickName;
    [MTGlobalValue sharedInstance].password = self.password;
    [MTGlobalValue sharedInstance].isMale = self.isMale;
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",nil];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSDictionary * dic = @{@"username":self.textField1.text,
                           @"sex":@(self.isMale?1:2),
                           @"password":self.password,
                           @"phone":self.registerPhoneNum,
                           @"source":@"0"
                           };
    [manager POST:REGISTER parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        NSLog(@"success~");
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];

//    WEAK_SELF;
//    [manager POST:REGISTER parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//        STRONG_SELF;
//        [formData appendPartWithFormData:[self.textField1.text dataUsingEncoding:NSUTF8StringEncoding] name:@"username"];
//        [formData appendPartWithFormData:[[NSString stringWithFormat:@"%d",self.isMale] dataUsingEncoding:NSUTF8StringEncoding] name:@"sex"];
//        [formData appendPartWithFormData:[self.password dataUsingEncoding:NSUTF8StringEncoding] name:@"password"];
//        [formData appendPartWithFormData:[self.registerPhoneNum dataUsingEncoding:NSUTF8StringEncoding] name:@"phone"];
//        [formData appendPartWithFormData:[@"0" dataUsingEncoding:NSUTF8StringEncoding] name:@"source"];
//
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
//        NSLog(@"success~");
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error.localizedDescription);
//    }];
    
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
    self.confirmedBtn.enabled = NO;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == _textField1) {
        self.nickName = textField.text;
    } else if (textField == _textField2) {
        self.tempPassword = textField.text;
    } else if (textField == _textField3) {
        self.password = textField.text;
    }
    [self judgeInfo];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _textField1) {
        if ([textField.text isEqualToString:@""]) {
            _remindLbl1.hidden = NO;
            self.confirmedBtn.enabled = NO;
            return YES;
        }
        _remindLbl1.hidden = YES;
        [_textField1 resignFirstResponder];
        [_textField2 becomeFirstResponder];
    } else if (textField == _textField2) {
        if ([textField.text isEqualToString:@""]) {
            _remindLbl2.hidden = NO;
            _remindLbl3.hidden = YES;
            self.confirmedBtn.enabled = NO;
            return YES;
        }
        _remindLbl2.hidden = YES;
        [_textField2 resignFirstResponder];
        [_textField3 becomeFirstResponder];
    } else if (textField == _textField3) {
        if (![self.tempPassword isEqualToString:textField.text]) {
            _remindLbl3.hidden = NO;
            _textField2.text = @"";
            _textField3.text = @"";
            self.tempPassword = @"";
            self.password = @"";
            self.confirmedBtn.enabled = NO;
            [_textField3 resignFirstResponder];
            [_textField2 becomeFirstResponder];
            return YES;
        } else if (![textField.text isEqualToString:@""]) {
            _remindLbl3.hidden = YES;
            [_textField3 resignFirstResponder];
        }
    }
    return YES;
}

#pragma mark - Help
- (void)judgeInfo
{
    if (![self.nickName isEqualToString:@""] && self.sexChosen == YES && [self.tempPassword isEqualToString:self.password] && ![self.tempPassword isEqualToString:@""]) {
        self.confirmedBtn.enabled = YES;
    } else {
        self.confirmedBtn.enabled = NO;
    }
}

@end





